/**
 * 
 */
package com.cn.practice.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合模式 -组合模式通常用来解决树形结构数据相关问题
 * 
 * @author zhangyang
 *
 */
public class CompositePractice2 {
	public static void main(String[] args) {
		org org = new org("亚信科技","亚信科技");
		sub sub1 = new sub("CMC事业部","CMC事业部");
		sub1.add(new leaf("MSP事业部","MSP事业部"));
		sub1.add(new leaf("ASM事业部","ASM事业部"));
		org.add(sub1);
		sub sub2 = new sub("AOF事业部","aof事业部");
		sub2.add(new leaf("MSP事业部","MSP事业部"));
		sub2.add(new leaf("ASM事业部","ASM事业部"));
		org.add(sub2);
//		org.printAll();
		sub2.printAll();
		
	}
}

abstract class MenuResource {
	String name;
	String desc;
	
	

	public MenuResource(String name, String desc) {
		super();
		this.name = name;
		this.desc = desc;
	}

	abstract void add(MenuResource menu);

	abstract void remove(MenuResource menu);

	abstract void update(MenuResource menu);

	abstract void printAll();

	@Override
	public String toString() {
		return "MenuResource [name=" + name + ", desc=" + desc + "]";
	}
	
	
}

/**
 * 一级部门 非叶节点-有集合存储下级数据
 * 
 * @author zhangyang
 *
 */
class org extends MenuResource {

/**
	 * @param name
	 * @param desc
	 */
	public org(String name, String desc) {
		super(name, desc);
		// TODO Auto-generated constructor stub
	}

List<MenuResource> next=new ArrayList<>();

	@Override
	void add(MenuResource menu) {
		// TODO Auto-generated method stub
		next.add(menu);
	}

	@Override
	void remove(MenuResource menu) {
		// TODO Auto-generated method stub
		next.remove(menu);
	}

	@Override
	void update(MenuResource menu) {
		// TODO Auto-generated method stub
	}

	@Override
	void printAll() {
		// TODO Auto-generated method stub
		System.out.println("-----一级部门-------");
		System.out.println(this);
		for(MenuResource menu:next) {
			System.out.println("-----二级部门-------");
			System.out.println(menu);
			menu.printAll();
		}
	}

}

/**
 * 二级部门-非叶节点
 * @author zhangyang
 *
 */
class sub extends MenuResource{
	
	/**
	 * @param name
	 * @param desc
	 */
	public sub(String name, String desc) {
		super(name, desc);
		// TODO Auto-generated constructor stub
	}

	List<MenuResource> next=new ArrayList<>();

	@Override
	void add(MenuResource menu) {
		// TODO Auto-generated method stub
		next.add(menu);
	}

	@Override
	void remove(MenuResource menu) {
		// TODO Auto-generated method stub
		next.remove(menu);
	}

	@Override
	void update(MenuResource menu) {
		// TODO Auto-generated method stub
	}

	@Override
	void printAll() {
		// TODO Auto-generated method stub
		System.out.println("-----三级部门-------");
		for(MenuResource menu:next) {
			System.out.println(menu);
			menu.printAll();
		}
	}
}

/**
 * 三级部门-叶节点没有集合包含下各部门 只有自身
 * @author zhangyang
 *
 */
class leaf extends MenuResource{

	/**
	 * @param name
	 * @param desc
	 */
	public leaf(String name, String desc) {
		super(name, desc);
		// TODO Auto-generated constructor stub
	}

	@Override
	void add(MenuResource menu) {
		// TODO Auto-generated method stub
	}

	@Override
	void remove(MenuResource menu) {
		// TODO Auto-generated method stub
	}

	@Override
	void update(MenuResource menu) {
		// TODO Auto-generated method stub
	}

	@Override
	void printAll() {
		// TODO Auto-generated method stub
//		System.out.println("-----三级部门-------");
//		System.out.println(this);
	}
}